package com.dnzx.controller.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.model.OrderBO;
import com.dnzx.service.IOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/web/order")
@Api(value = "/web/order",description="订单",produces=MediaType.APPLICATION_JSON_VALUE)
public class WebOrderController {

	@Resource
	private IOrderService orderService;

	/**
	 * 下单
	 * @param orderBO
	 * @return
	 */
	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result save(@ApiParam(value = "实体") @RequestBody @Valid OrderBO orderBO, HttpServletRequest request) {
		try{
			this.orderService.save(orderBO);
	        return ResultUtils.returnSuccess("SUCCESS", orderBO);
        }catch (Exception e){
        	e.printStackTrace();
            return ResultUtils.returnError(e.getMessage());
        }
    }
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
	@GetMapping("/get/{sn}")
	@ApiOperation(value = "/get/{sn}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "sn") @PathVariable("sn") String sn, HttpServletRequest request) {
		try {
			OrderBO orderBOQuery = new OrderBO();
			orderBOQuery.setSn(sn);
			OrderBO orderBO = this.orderService.get(orderBOQuery);
			return ResultUtils.returnSuccess("SUCCESS", orderBO);
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id, HttpServletRequest request){
		try {
			this.orderService.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}

	/**
	 * 修改
	 * @param orderBO
	 * @return
	 */
	@PostMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result update(@ApiParam(value = "实体") @RequestBody OrderBO orderBO, HttpServletRequest request) {
		try {
			this.orderService.update(orderBO);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError(e.getMessage());
		}
	}


	/**
	 * 查询 - 分页
	 * @param orderBO
	 * @return
	 */
	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody OrderBO orderBO, HttpServletRequest request) {
		try {
			PageResult<List<OrderBO>> pageResult = new PageResult<List<OrderBO>>();
			pageResult = this.orderService.queryPage(orderBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError(e.getMessage());
		}
	}

}
